package com.bjsxt.service.impl;

import com.bjsxt.mapper.ExpenseItemMapper;
import com.bjsxt.mapper.ExpenseMapper;
import com.bjsxt.pojo.Expense;
import com.bjsxt.pojo.ExpenseItem;
import com.bjsxt.service.ExpenseService;
import com.bjsxt.util.SqlsesionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ExpenseServiceImpl implements ExpenseService {
    @Override
    public int save(Expense expense, List<ExpenseItem> list) {
        SqlSession sqlSession = null;
        int insert = 0;
        try {
             sqlSession = SqlsesionUtil.getSqlSession();
             sqlSession.getMapper(ExpenseMapper.class).insert(expense);
            ExpenseItemMapper mapper = sqlSession.getMapper(ExpenseItemMapper.class);
            for (ExpenseItem expenseItem:list) {
                //把报销单ID设置到报销单明细中
                expenseItem.setExpId(expense.getExpId());
                mapper.insert(expenseItem);
            }
            insert=1;
            sqlSession.commit();//如果都没有错误，最后提交整个事务
        } catch (Exception e) {
           sqlSession.rollback();//一旦有错误直接回滚即可
        } finally {
            sqlSession.close();
        }

        return insert;
    }

    @Override
    public List<Expense> findMore(String empid) {
        SqlSession sqlSession = SqlsesionUtil.getSqlSession();
        List<Expense> list = sqlSession.getMapper(ExpenseMapper.class).selectMore(empid);
        sqlSession.close();

        return list;
    }
}
